using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful.Amazon.A2S;

namespace CodePlex.Resourceful.Examples
{
    public static class A2SExample
    {

        public static void Run()
        {

//http://ecs.amazonaws.com/onca/xml?
//Service=AWSECommerceService&
//Operation=ItemSearch&
//AWSAccessKeyId=[Access Key ID]&
//AssociateTag=[ID]&
//SearchIndex=Apparel&
//Keywords=Shirt



            string aws = Environment.GetEnvironmentVariable("AWS");

            string accessKeyID = aws.Split(':')[0];
            string secretAccessKey = aws.Split(':')[1];


            A2SClient client = new A2SClient(accessKeyID);
            client.Tracer.AddListener(TraceTextWriter.Instance);


            ItemSearchResponse response = client.ItemSearch("MP3Downloads", "B000SX6JKO", 1);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(response.OperationRequest.RequestID);
            Console.WriteLine(response.OperationRequest.RequestProcessingTime);
            Console.WriteLine(response.OperationRequest.HttpHeaders);
            Console.WriteLine(response.OperationRequest.Arguments);

            Console.WriteLine(response.Items.Request.IsValid);
            Console.WriteLine(response.Items.Request.ItemSearchRequest);
            foreach (Error error in response.Items.Request.Errors)
            {
                Console.WriteLine(error.Code + " " + error.Message);
            }


            Console.WriteLine(response.Items.TotalPages);
            Console.WriteLine(response.Items.TotalResults);

            foreach (Item item in response.Items)
            {

               
                Console.WriteLine(item.ASIN);
                Console.WriteLine(item.ASINFromDetailPageURL);

            
                Console.WriteLine(item.DetailPageURL);
                Console.WriteLine(item.ItemAttributes);

                Console.WriteLine(item.SmallImage);
                Console.WriteLine(item.MediumImage);
                Console.WriteLine(item.LargeImage);
                foreach (ImageSet imageSet in item.ImageSets)
                {
                    Console.WriteLine(imageSet.Category);
                    foreach (string key in imageSet.Keys)
                    {
                        Console.WriteLine("{0}:{1}", key, imageSet[key]);
                    }
                }
                Console.WriteLine(item.OfferSummary.LowestNewPrice);
                Console.WriteLine("{0} {1} {2} {3}",
                    item.OfferSummary.TotalCollectible,
                    item.OfferSummary.TotalNew,
                    item.OfferSummary.TotalRefurbished,
                    item.OfferSummary.TotalUsed);

                Console.WriteLine("{0} {1}",
                  item.Offers.TotalOffers,
                  item.Offers.TotalOfferPages);

                foreach (BrowseNode browseNode in item.BrowseNodes)
                {
                    Console.WriteLine(browseNode.FullName);
                }
                

            }

            if (response.Items.Count != 1)
            {
                return;
            }
           
           
        }


       


    }
}
